package com.vfmall.service.impl;
import com.vfmall.context.BaseContext;
import com.vfmall.dto.ShoppingCartDTO;
import com.vfmall.entity.Dish;
import com.vfmall.entity.ShoppingCart;
import com.vfmall.mapper.DishMapper;
import com.vfmall.mapper.SetmealMapper;
import com.vfmall.mapper.ShoppingCartMapper;
import com.vfmall.service.ShoppingCartService;
import com.vfmall.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    ShoppingCartMapper shoppingCartMapper;

    @Autowired
    DishMapper dishMapper;

    @Autowired
    SetmealMapper setmealMapper;

    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        // 获取当前上下文用户id
        shoppingCart.setUserId( BaseContext.getCurrentId());
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
            List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if (shoppingCart!=null && list.size()>0) {
            shoppingCart = list.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updateNumber(shoppingCart);
        }else {
            if (shoppingCartDTO.getSetmealId() != null){
                SetmealVO byId = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setAmount(byId.getPrice());
                shoppingCart.setName(byId.getName());
                shoppingCart.setImage(byId.getImage());
            }else {
                Dish byId = dishMapper.getById(shoppingCartDTO.getDishId());
                shoppingCart.setAmount(byId.getPrice());
                shoppingCart.setName(byId.getName());
                shoppingCart.setImage(byId.getImage());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            shoppingCartMapper.insert(shoppingCart);
        }

    }

    @Override
    public List<ShoppingCart> showShoppingCart() {
        ShoppingCart build = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        return shoppingCartMapper.list(build);
    }

    @Override
    public void cleanShoppingCart() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        shoppingCart = list.get(0);
        if (shoppingCart.getNumber()==1) {
            shoppingCartMapper.deleteById(shoppingCart.getId());
        }else {
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            shoppingCartMapper.updateNumber(shoppingCart);
        }

    }
}
